/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.aun.news;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.ioc.RequestScoped;
import br.com.caelum.vraptor.view.Results;
import br.org.groupwareworkbench.collablet.coop.recommend.RecommendMgrController;
import br.org.groupwareworkbench.collablet.coop.recommend.RecommendMgrInstance;
import br.org.groupwareworkbench.core.framework.CollabElementInstance;
import br.org.groupwareworkbench.core.framework.CollabletInstance;
import br.org.groupwareworkbench.core.bd.GenericEntity;

import java.util.ArrayList;

@RequestScoped
@Resource
public class NewsMgrController {

    private final Result result;
    private final HttpServletRequest request;

    public NewsMgrController(Result result, HttpServletRequest request) {
        this.result = result;
        this.request = request;
    }

    @Get
    @Path(value = "/groupware-workbench/{newsMgr}/news")
    public List<News> list(NewsMgrInstance newsMgr) {
        addIncludes(newsMgr);
        return newsMgr.list();
    }

    @Post
    @Get
    @Path(value = "/groupware-workbench/{newsMgr}/news/{id}/show")
    public News show(final CollabletInstance collablet, final NewsMgrInstance newsMgr, final long id) {
        result.include("nameCollablet","news");
        addIncludes(newsMgr);

        if (id == 0) return new News();

        News toReturn = newsMgr.findById(id);

        newsMgr.processWidgets(request, toReturn);

        return toReturn;
    }

    @Get
    @Path(value = "/groupware-workbench/{newsMgr}/news/{id}")
    public News retrieve(NewsMgrInstance newsMgr, long id) {
        addIncludes(newsMgr);
        if (id != 0) return newsMgr.findById(id);
        return new News();
    }

    @Post
    @Path(value = "/groupware-workbench/{newsMgr}/news")
    public void save(NewsMgrInstance newsMgr, final News news) {
        newsMgr.save(news);

        newsMgr.processWidgets(request, news);

        RecommendMgrController recommendMgr = new RecommendMgrController(result);
        List<News> newsList = newsMgr.list();
        List<GenericEntity> newsListGE = new ArrayList<GenericEntity>();
        for (News news2 : newsList) {
            newsListGE.add(news2);
        }
        recommendMgr.generateRecommend(newsMgr.getCollabElementInstance(RecommendMgrInstance.class), newsMgr, newsListGE, news);
        result.use(Results.logic()).redirectTo(NewsMgrController.class).list(newsMgr);
    }

    @Delete
    @Path(value = "/groupware-workbench/{collablet}/news/{news.id}")
    public void delete(NewsMgrInstance collablet, final News news) {
        collablet.delete(news.getId());
        result.use(Results.logic()).redirectTo(NewsMgrController.class).list(collablet);
    }

    private void addIncludes(NewsMgrInstance collablet) {
        result.include("newsMgr", collablet);
        for (CollabElementInstance collabComponentInstance : collablet.getCollabElementInstances()) {
            String nomeComponente = collabComponentInstance.getName();
            result.include(nomeComponente, collabComponentInstance);
            System.out.println("O componente " + collabComponentInstance.getComponent().getCod() +
                    " foi adicionado na requisição com o nome " + nomeComponente);
        }
    }
}